package com.atkxp.sys.controller;

import com.atkxp.sys.annotation.Log;
import com.atkxp.sys.entity.Result;
import com.atkxp.sys.entity.Role;
import com.atkxp.sys.entity.User;
import com.atkxp.sys.enums.BusinessType;
import com.atkxp.sys.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kxp
 * @since 2023-08-26
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/getAllRole")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<List<Role>> getAllRole() {
        return Result.success(roleService.list(), "查询成功");
    }


    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<Map<String, Object>> queryAllRole(@RequestParam(value = "roleName", required = false) String roleName,
                                                    @RequestParam(value = "pageNo") Long pageNo,
                                                    @RequestParam(value = "pageSize") Long pageSize) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(roleName), Role::getRoleName, roleName);
        Page<Role> page = new Page<>(pageNo, pageSize);
        roleService.page(page, wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:role:list')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    public Result<?> addRole(@RequestBody Role role) {
//        user.setPassword(passwordEncoder.encode(user.getPassword()));
        roleService.addRole(role);
        return Result.success("新增角色成功");
    }

    @PostMapping("/update")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<?> updateRole(@RequestBody Role role) {
        roleService.updateRole(role);
        return Result.success("修改用户成功");
    }

    @GetMapping("/getRoleById/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<Role> getRoleById(@PathVariable("roleId") Integer roleId) {
        Role role = roleService.getRoleById(roleId);
        return Result.success(role);
    }

    @DeleteMapping("/delete/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:list')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    public Result<?> deleteRoleById(@PathVariable("roleId") Integer roleId) {
        roleService.deleteRoleById(roleId);
        return Result.success("删除成功！");
    }
}
